package com.xianbeiying.app.ckgl.widght;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AppManager {

    public static AppManager instance;
    private WindowSizeEntity windowSizeEntity;
    private ExecutorService loadThreadPool;
    private int threadSize = 0;
    private int mainBannerLayoutRes;

    public void init(Context context) {
        // 初始化线程池
        loadThreadPool = Executors
                .newFixedThreadPool(threadSize > 0 ? threadSize : Runtime
                        .getRuntime().availableProcessors() * 5);
        initWidthAndHeight(context);
    }

    public void setThreadSize(int threadSize) {
        this.threadSize = threadSize;
    }

    public static AppManager getInstance() {
        if (instance == null) {
            synchronized (AppManager.class) {
                if (instance == null) {
                    instance = new AppManager();
                }
            }
        }
        return instance;
    }

    /**
     * 线程池
     *
     * @return
     */
    public ExecutorService getLoadThreadPool() {
        return loadThreadPool;
    }

    /**
     * 获取屏幕对象
     *
     * @return
     */
    public WindowSizeEntity getWindowSizeEntity() {
        return windowSizeEntity;
    }

    /**
     * 初始化屏幕宽高
     */
    private void initWidthAndHeight(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        wManager.getDefaultDisplay().getMetrics(dm);
        windowSizeEntity = new WindowSizeEntity(dm.widthPixels, dm.heightPixels);
    }

    public int getMainBannerLayoutRes() {
        return mainBannerLayoutRes;
    }

    public void initMainBannerLayoutRes(int mainBannerLayoutRes) {
        this.mainBannerLayoutRes = mainBannerLayoutRes;
    }
    /**
     *  动态设置组件的大小
     * @param view  组件
     * @param height 高
     * @param width 宽
     */
    @SuppressWarnings("unused")
    private static void setLayoutScale(View view,int height,int width)
    {
    }
    /**
     * 获取应用程序名称
     */
    public static String getAppName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取应用程序版本名称信息
     *
     * @param context
     * @return 当前应用的版本名称
     */
    public static String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取应用程序版本名称信息
     *
     * @param context
     * @return 当前应用的版本名称
     */
    public static int getVersionCode(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionCode;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
